{% extends "myhome_base.html" %}

{% load i18n avatar_tags seahub_tags %}

{% block sub_title %}{{repo.name}} - {% endblock %}

{% block extra_style %}
<style type="text/css">
#left-panel { position: relative; }
.go-back { top:0; }
</style>
{% endblock %}

{% block left_panel %}
<a class="go-back" title="{% trans "Back to Library" %}" href="{% url 'repo' repo.id %}"><span class="icon-chevron-left"></span></a>
<div class="side-textnav">
    <ul class="side-textnav-tabs">
        <li class="tab tab-cur"><a href="#basic-info">{% trans "Basic Info" %}</a></li>
        <li class="tab"><a href="#transfer-ownership">{% trans "Transfer Ownership" %}</a></li>
        {% if repo.encrypted and repo.enc_version == 2 %}
        <li class="tab"><a href="#change-password">{% trans "Change Password" %}</a></li>
        {% endif %}
        {% if not repo.encrypted %}
        <li class="tab"><a href="#shared-links">{% trans "Shared Links" %}</a></li>
        {% endif %}
    </ul>
</div>
{% endblock %}

{% block right_panel %}
<div class="lib-setting">
    <h2>{% trans "Library Settings" %}</h2>

    <div id="basic-info" class="setting-item">
        <h3>{% trans "Basic Info" %}</h3>
        <form id="repo-basic-info-form" action="" method="post" class="form">{% csrf_token %}
            <label>{% trans "Name" %}</label><br />
            <input type="text" name="repo_name" value="{{ repo.name }}" class="input" /><br />
            <label>{% trans "Description" %}</label></br />
            <textarea name="repo_desc" class="textarea">{{ repo.desc }}</textarea><br />

            {% if not ENABLE_SUB_LIBRARY or not repo.is_virtual %}
            <label>{% trans "History" %}</label><br />
            <input type="radio" name="history" value="full_history" {% if full_history_checked %}checked="checked"{% endif %} class="vam" {% if not full_history_enabled %}disabled="disabled"{% endif %} /> <span class="vam">{% trans "Keep full history" %}</span><br />
            <input type="radio" name="history" value="no_history" {% if no_history_checked %}checked="checked"{% endif %} class="vam" {% if not full_history_enabled %}disabled="disabled"{% endif %} /> <span class="vam">{% trans "Don't keep history" %}</span><br />
            <input type="radio" name="history" value="partial_history" {% if partial_history_checked %}checked="checked"{% endif %} class="vam" {% if not full_history_enabled %}disabled="disabled"{% endif %} /> <span calss="vam">{% trans "Only keep a period of history:" %}
                <input type="text" name="days" size="4" {% if not days_enabled %} disabled="disabled" class="input-disabled"{% endif %} value="{{history_limit}}" /> {% trans "days" %}</span><br />
            {% endif %}

            <p class="error hide"></p>
            <input type="submit" value="{% trans "Submit" %}" class="submit" />
        </form>
    </div>

    <div id="transfer-ownership" class="setting-item hide">
        <h3>{% trans "Transfer Ownership" %}</h3>
        <form id="repo-owner-form" action="" method="post" class="form">{% csrf_token %}
            <p>{% trans "Transfer this library to another user:" %}</p>
            <input type="text" name="repo_owner" value="" placeholder="{% trans "Email" %}" class="input" /><br />
            <p class="error hide"></p>
            <input type="submit" value="{% trans "Submit" %}" class="submit" />
        </form>
    </div>

    {% if repo.encrypted and repo.enc_version == 2 %}
    <div id="change-password" class="setting-item hide">
        <h3>{% trans "Change Password" %}</h3>
        <form id="repo-change-passwd-form" action="" method="post" class="form">{% csrf_token %}
            <p>{% trans "Change the password of this library:" %}</p>
            <label>{% trans "Old Password" %}</label><br />
            <input type="password" name="old_passwd" class="input" /><br />
            <label>{% blocktrans %}New Password(at least {{repo_password_min_length}} characters){% endblocktrans %}</label><br />
            <input type="password" name="new_passwd" class="input" /><br />
            <label>{% trans "New Password Again" %}</label><br />
            <input type="password" name="new_passwd_again" class="input" /><br />
            <p class="error hide"></p>
            <input type="submit" value="{% trans "Submit" %}" class="submit" />
        </form>
    </div>
    {% endif %}

    {% if not repo.encrypted %}
    <div id="shared-links" class="setting-item hide">
        <h3>{% trans "Shared Links" %}</h3>
        <p>{% trans "View and manage all the shared links in this library." %}</p>
        <div id="tabs" class="tab-tabs">
            <ul class="tab-tabs-nav hd ovhd">
                <li class="tab"><a href="#upload-links" class="a">{% trans "Upload Links" %}</a></li>
                <li class="tab"><a href="#download-links" class="a">{% trans "Download Links" %}</a></li>
            </ul>
            <div id="upload-links">
                <table>
                    <tr>
                        <th width="5%"><!--icon--></th>
                        <th width="40%">{% trans "Name"%}</th>
                        <th width="15%">{% trans "Created By"%}</th>
                        <th width="15%">{% trans "Size"%}</th>
                        <th width="12%">{% trans "Visits"%}</th>
                        <th width="13%">{% trans "Operations"%}</th>
                    </tr>
                    {% if uploadlinks %}
                    {% for link in uploadlinks %}
                    <tr>
                        <td class="alc"><img src="{{ MEDIA_URL }}img/folder-icon-24.png" alt="{% trans "Directory icon"%}" /></td>
                        <td><a href="{% url 'repo' repo.id %}?p={{ link.path|urlencode }}">{{ link.dir_name }}</a></td>
                        <td><a href="{% url 'user_profile' link.username %}">{{ link.username|email2nickname }}</a></td>
                        <td>--</td>
                        <td>{{ link.view_cnt }}</td>
                        <td>
                            <span class="icon-trash op-icon vh rm-link" data-token={{ link.token }} data-type="upload" title="{% trans "Remove"%}"></span>
                        </td>
                    </tr>
                    {% endfor %}
                    {% endif %}
                </table>
            </div>
            <div id="download-links" class="hide">
                <table>
                    <tr>
                        <th width="5%"><!--icon--></th>
                        <th width="40%">{% trans "Name"%}</th>
                        <th width="15%">{% trans "Created By"%}</th>
                        <th width="15%">{% trans "Size"%}</th>
                        <th width="12%">{% trans "Visits"%}</th>
                        <th width="13%">{% trans "Operations"%}</th>
                    </tr>
                    {% if fileshares %}
                    {% for link in fileshares %}
                    <tr>
                        {% if link.s_type == 'd'%}
                        <td class="alc"><img src="{{ MEDIA_URL }}img/folder-icon-24.png" alt="{% trans "Directory icon"%}" /></td>
                        <td><a href="{% url 'repo' repo.id %}?p={{ link.path|urlencode }}">{{ link.filename }}</a></td>
                        <td><a href="{% url 'user_profile' link.username %}">{{ link.username|email2nickname }}</a></td>
                        {% else %}
                        <td class="alc"><img src="{{ MEDIA_URL }}img/file/{{ link.filename|file_icon_filter }}" alt="{% trans "File"%}" /></td>
                        <td><a href="{% url 'repo_view_file' repo.id %}?p={{ link.path|urlencode }}">{{ link.filename }}</a></td>
                        <td><a href="{% url 'user_profile' link.username %}">{{ link.username|email2nickname }}</a></td>
                        {% endif %}
                        <td>{{ link.filesize|filesizeformat}}</td>
                        <td>{{ link.view_cnt }}</td>
                        <td>
                            <span class="icon-trash op-icon vh rm-link" data-token={{ link.token }} data-type="download" title="{% trans "Remove"%}"></span>
                        </td>
                    </tr>
                    {% endfor %}
                    {% endif %}
                </table>
            </div>
        </div>
    </div>
    {% endif %}

</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('#repo-basic-info-form input[name="history"]').change(function() {
    var value = $(this).attr('value'),
        days_input = $('#repo-basic-info-form input[name="days"]');
    if (value == 'full_history' || value == 'no_history') {
        days_input.attr('disabled', true).addClass('input-disabled');
    } else {
        days_input.attr('disabled', false).removeClass('input-disabled');
    }
});

$('#repo-basic-info-form').submit(function() {
    var form = $(this),
        form_id = form.attr('id');
    var repo_name = $('[name="repo_name"]', form).val(),
        repo_desc = $('[name="repo_desc"]', form).val();

    if (!$.trim(repo_name)) {
        apply_form_error(form_id, "{% trans "Name is required." %}");
        return false;
    }
    if (!$.trim(repo_desc)) {
        apply_form_error(form_id, "{% trans "Description is required." %}");
        return false;
    }

    var days;
    var value = $(this).find('input[name="history"]:checked').val();

    if (value == 'partial_history') {
        days = $(this).find('input[name="days"]').val();
    } else if (value == 'full_history') {
        days = -1;
    } else {
        days = 0;
    }

    var submit_btn = $(this).children('input[type="submit"]');
    disable(submit_btn);
    $.ajax({
        url: '{% url 'repo_change_basic_info' repo.id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'repo_name': repo_name,
            'repo_desc': repo_desc,
            'days': days
        },
        success: function(data) {
            if (data['success']) {
                location.reload(true);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.responseText) {
                apply_form_error(form_id, $.parseJSON(jqXHR.responseText).error);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(submit_btn);
        }
    });
    return false;
});

$('#repo-owner-form').submit(function() {
    var form = $(this),
        form_id = form.attr('id'),
        new_owner = $('[name="repo_owner"]', form).val(),
        submit_btn = $('input[type="submit"]', form);

    if (!$.trim(new_owner)) {
        return false;
    }

    disable(submit_btn);
    $.ajax({
        url: '{% url 'repo_transfer_owner' repo.id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'repo_owner': new_owner
        },
        success: function(data) {
            if (data['success']) {
                location.href = '{% url 'myhome' %}';
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.responseText) {
                apply_form_error(form_id, $.parseJSON(jqXHR.responseText).error);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(submit_btn);
        }
    });
    return false;
});

{% if repo.encrypted and repo.enc_version == 2 %}
$('#repo-change-passwd-form').submit(function() {
    var form = $(this),
        form_id = form.attr('id'),
        old_passwd, new_passwd, new_passwd_again;

    old_passwd = $('input[name="old_passwd"]', form).val();
    new_passwd = $('input[name="new_passwd"]', form).val();
    new_passwd_again = $('input[name="new_passwd_again"]', form).val();

    if (!$.trim(old_passwd)) {
        apply_form_error(form_id, "{% trans "Please enter the old password" %}");
        return false;
    }
    if (!$.trim(new_passwd)) {
        apply_form_error(form_id, "{% trans "Please enter the new password" %}");
        return false;
    }
    if ($.trim(new_passwd).length < {{repo_password_min_length}}) {
        apply_form_error(form_id, "{% trans "New password is too short" %}");
        return false;
    }
    if (!$.trim(new_passwd_again)) {
        apply_form_error(form_id, "{% trans "Please enter the new password again" %}");
        return false;
    }
    if ($.trim(new_passwd) != $.trim(new_passwd_again)) {
        apply_form_error(form_id, "{% trans "New passwords don't match" %}");
        return false;
    }

    var submit_btn = $(this).children('input[type="submit"]');
    disable(submit_btn);

    $.ajax({
        url: '{% url 'repo_change_passwd' repo.id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'old_passwd': old_passwd,
            'new_passwd': new_passwd,
            'new_passwd_again': new_passwd_again
        },
        success: function(data) {
            if (data['success']) {
                location.reload(true);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.responseText) {
                apply_form_error(form_id, $.parseJSON(jqXHR.responseText).error);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(submit_btn);
        }
    });
    return false;
});
{% endif %}

$('.side-textnav-tabs .tab').click(function() {
    var cur_tab = $(this),
        cur_tab_con = $($('a', cur_tab).attr('href'));

    $('.side-textnav-tabs .tab').removeClass('tab-cur');
    $('.lib-setting .setting-item').addClass('hide');
    cur_tab.addClass('tab-cur');
    cur_tab_con.removeClass('hide')
    return false;
});

{% if not repo.encrypted %}
$('#shared-links .rm-link').click(function() {
    var op = $(this),
        link_token = op.data('token'),
        link_type = op.data('type'),
        ajax_url;

    if (link_type == 'upload') {
        ajax_url = '{% url 'ajax_remove_shared_upload_link' %}';
    } else {
        ajax_url = '{% url 'ajax_remove_shared_link' %}';
    }

    $.ajax({
        url: ajax_url + '?t=' + e(link_token),
        dataType: 'json',
        success: function(data) {
            op.parents('tr').remove();
            feedback("{% trans "Removed successfully." %}", 'success');
        },
        error: ajaxErrorHandler
    });

    return false;
});
{% endif %}
</script>
{% endblock %}
